package tool.dbfile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import tool.Constant.DB_APP_FILE_TYPE;
import tool.Constant.DB_DBA_FILE_TYPE;
import tool.Constant.DB_FILE_TYPE;
import tool.Constant.PROJECT;
import tool.file.util.ReadFileUtil;
import tool.string.StringUtil;

public class MakeDeployFile {

    public static File makeFile(String filePath) {

        List<String> paths = getFullPath(filePath);

        List<String> dbaTables = new ArrayList<String>();
        List<String> dbaSequences = new ArrayList<String>();
        List<String> dbaForeigns = new ArrayList<String>();
        List<String> dbaIndexs = new ArrayList<String>();
        List<String> dbaPackages = new ArrayList<String>();
        List<String> appTables = new ArrayList<String>();
        List<String> appTypes = new ArrayList<String>();
        List<String> appSynonyms = new ArrayList<String>();
        List<String> appViews = new ArrayList<String>();
        List<String> appIndexs = new ArrayList<String>();
        List<String> appFunctions = new ArrayList<String>();
        List<String> appProcedures = new ArrayList<String>();
        List<String> appPackages = new ArrayList<String>();

        for (String s : paths) {
            List<String> files = ReadFileUtil.readAllFilesByExNm(s, ".sql");
            File f = new File(s);
            if (f.exists()) {
                if (isDbaTable(s)) {
                    dbaTables.addAll(files);
                } else if (isDbaSequences(s)) {
                    dbaSequences.addAll(files);
                } else if (isDbaForeigns(s)) {
                    dbaForeigns.addAll(files);
                } else if (isDbaIndexs(s)) {
                    dbaIndexs.addAll(files);
                } else if (isDbaPackages(s)) {
                    dbaPackages.addAll(files);
                } else if (isAppTables(s)) {
                    appTables.addAll(files);
                } else if (isAppTypes(s)) {
                    appTypes.addAll(files);
                } else if (isAppSynonyms(s)) {
                    appSynonyms.addAll(files);
                } else if (isAppViews(s)) {
                    appViews.addAll(files);
                } else if (isAppIndexs(s)) {
                    appIndexs.addAll(files);
                } else if (isAppFunctions(s)) {
                    appFunctions.addAll(files);
                } else if (isAppProcedures(s)) {
                    appProcedures.addAll(files);
                } else if (isAppPackages(s)) {
                    appPackages.addAll(files);
                }
            }
        }

        for (String ss : dbaTables) {
            System.out.println(ss);
        }

        return null;
    }

    private static Boolean isRightFolder(String parentNm, String nm, String path) {

        File f = new File(path);
        if (!f.isDirectory()) {
            return false;
        }

        if (StringUtil.isBlankOrNull(nm) || StringUtil.isBlankOrNull(parentNm)) {
            return false;
        }

        String parentNm_ = getParentName(path).toLowerCase();
        String folderNm_ = getName(path).toLowerCase();

        return parentNm.equals(parentNm_) && nm.equals(folderNm_);
    }

    private static Boolean isDbaRightFolder(String folderNm, String path) {

        if (isRightFolder("dba", folderNm, path)) {
            return true;
        }

        return false;
    }

    private static Boolean isAppRightFolder(String folderNm, String path) {

        if (isRightFolder("app", folderNm, path)) {
            return true;
        }

        return false;
    }

    private static Boolean isDbaTable(String path) {

        if (isDbaRightFolder("tables", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isDbaSequences(String path) {

        if (isDbaRightFolder("sequences", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isDbaForeigns(String path) {

        if (isDbaRightFolder("foreigns", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isDbaIndexs(String path) {

        if (isDbaRightFolder("indexs", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isDbaPackages(String path) {

        if (isDbaRightFolder("packages", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppTables(String path) {

        if (isAppRightFolder("tables", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppTypes(String path) {

        if (isAppRightFolder("types", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppSynonyms(String path) {

        if (isAppRightFolder("synonyms", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppViews(String path) {

        if (isAppRightFolder("views", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppIndexs(String path) {

        if (isAppRightFolder("indexs", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppFunctions(String path) {

        if (isAppRightFolder("functions", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppProcedures(String path) {

        if (isAppRightFolder("procedures", path)) {
            return true;
        }
        return false;
    }

    private static Boolean isAppPackages(String path) {

        if (isAppRightFolder("packages", path)) {
            return true;
        }
        return false;
    }

    private static String getName(String path) {

        return new File(path).getName();
    }

    private static String getParentName(String path) {

        return new File(path).getParentFile().getName();
    }

    private static List<String> getFullPath(String filePath) {

        List<String> list = new ArrayList<String>();

        List<String> projs = getProjPath(filePath);
        List<String> subPaths = getSubPath();
        for (String proj : projs) {
            for (String subPath : subPaths) {
                StringBuilder sb = new StringBuilder();
                sb.append(proj);
                sb.append(subPath);
                list.add(sb.toString());
            }
        }

        return list;
    }

    private static List<String> getProjPath(String filePath) {

        List<String> files = new ArrayList<String>();
        for (PROJECT proj : PROJECT.values()) {
            StringBuilder sb = new StringBuilder();
            sb.append(filePath);
            sb.append(File.separator);
            sb.append(proj.getName());
            sb.append(File.separator);

            files.add(sb.toString());
        }

        return files;
    }

    private static List<String> getSubPath() {

        List<String> list = new ArrayList<String>();
        for (DB_FILE_TYPE allType : DB_FILE_TYPE.values()) {
            if (allType.getFileType().equals(DB_FILE_TYPE.APP.getFileType())) {
                for (DB_APP_FILE_TYPE app : DB_APP_FILE_TYPE.values()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(allType.getFileType());
                    sb.append(File.separator);
                    sb.append(app.getFileType());
                    sb.append(File.separator);

                    list.add(sb.toString());
                }
            }
            if (allType.getFileType().equals(DB_FILE_TYPE.DBA.getFileType())) {
                for (DB_DBA_FILE_TYPE dba : DB_DBA_FILE_TYPE.values()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(allType.getFileType());
                    sb.append(File.separator);
                    sb.append(dba.getFileType());
                    sb.append(File.separator);

                    list.add(sb.toString());
                }
            }
        }

        return list;
    }
}
